home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / misc / ObtainGIRPort.lha / NewObtainGIRPort.a < prev    next >
Text File  |  1995-01-29  |  3KB  |  194 lines

  1. ; Patch ObtainGIRPort() to fix MagicMenu
  2.  
  3.     INCLUDE    'intuition/cghooks.i'
  4.  
  5. ObtainGIRPort    EQU    -$22E
  6. DisplayBeep        EQU    -$60
  7.  
  8. OpenLibrary        EQU    -$228
  9. CloseLibrary    EQU    -$19E
  10. SetFunction        EQU    -$1A4
  11. Forbid            EQU    -$84
  12. Permit            EQU    -$8A
  13. Wait            EQU    -$13E
  14. CacheClearU        EQU    -$27c
  15.  
  16. Delay            EQU -$C6
  17. PutStr            EQU    -$3B4
  18.  
  19. AttemptLockLayerRom    EQU    -$28E
  20. UnlockLayerRom    EQU    -$1B6
  21.     
  22. SIGBREAKF_CTRL_C    EQU 1<<12
  23.     
  24.     section text,code
  25.     
  26. _PatchOGR:
  27.     moveq    #20,d7
  28.  
  29.     ; Open libraries    
  30.     lea        gfxname(pc),a1
  31.     moveq    #0,d0
  32.     move.l    4,a6
  33.     jsr        OpenLibrary(a6)
  34.     lea        GfxBase(pc),a0
  35.     move.l    d0,(a0)
  36.     beq        fail1
  37.     
  38.     lea        dosname(pc),a1
  39.     moveq    #0,d0
  40.     jsr        OpenLibrary(a6)
  41.     lea        DOSBase(pc),a0
  42.     move.l    d0,(a0)
  43.     beq        fail2
  44.     
  45.     lea        intuiname(pc),a1
  46.     moveq    #36,d0
  47.     jsr        OpenLibrary(a6)
  48.     lea        IBase(pc),a0
  49.     move.l    d0,(a0)
  50.     beq        fail3
  51.     
  52.     jsr        Forbid(a6)
  53.  
  54.     ; Install patch
  55.     move.l    d0,a1
  56.     lea        newObtainGIRPort(pc),a0
  57.     move.l    a0,d0
  58.     move.l    #ObtainGIRPort,a0
  59.     jsr        SetFunction(a6)
  60.     lea        oldObtainGIRPort(pc),a0
  61.     move.l    d0,(a0)
  62.     
  63.     ; Clear CPU caches to flush data/instruction cache incoherence
  64.     jsr        CacheClearU(a6)
  65.     
  66.     jsr        Permit(a6)
  67.  
  68. loop:
  69.     move.l    4,a6
  70.     
  71.     ; Wait for ctrl-C
  72.     move.l    #SIGBREAKF_CTRL_C,d0
  73.     jsr        Wait(a6)
  74.     
  75.     jsr        Forbid(a6)
  76.  
  77.     ; Attempt to remove the patch
  78.     move.l    IBase(pc),a1
  79.     move.l    oldObtainGIRPort(pc),d0
  80.     move.l    #ObtainGIRPort,a0
  81.     jsr        SetFunction(a6)
  82.     lea        newObtainGIRPort(pc),a0
  83.     cmp.l    a0,d0
  84.     beq        ok
  85.     
  86.     ; Oops, someone has patched over, put it back
  87.     move.l    IBase(pc),a1
  88.     move.l    #ObtainGIRPort,a0
  89.     jsr        SetFunction(a6)
  90.     jsr        Permit(a6)
  91.     
  92.     ; We can't quit, so notify user with a beep
  93.     move.l    IBase(pc),a6
  94.     move.l    #0,a0
  95.     jsr        DisplayBeep(a6)
  96.     
  97.     bra        loop
  98.     
  99. ok:
  100.     jsr        Permit(a6)
  101.  
  102.     move.l    DOSBase(pc),a6
  103.  
  104.     ; Notify user of subsequent quit
  105.     lea        exitmsg(pc),a0
  106.     move.l    a0,d1
  107.     jsr     PutStr(a6)
  108.     
  109.     ; Wait a second to let other tasks exit from the patch code
  110.     ; We can't be absolutely sure but this should be enough of a safeguard
  111.     moveq    #50,d1
  112.     jsr        Delay(a6)
  113.     
  114.     moveq    #0,d7
  115.  
  116.     ; Close libraries and exit    
  117.     move.l    4,a6
  118.     move.l    IBase(pc),a1
  119.     jsr        CloseLibrary(a6)
  120.     
  121. fail3:
  122.     move.l    DOSBase(pc),a1
  123.     jsr        CloseLibrary(a6)
  124.     
  125. fail2:
  126.     move.l    GfxBase(pc),a1
  127.     jsr        CloseLibrary(a6)
  128.     
  129. fail1:
  130.     move.l    d7,d0
  131.     rts
  132.  
  133. newObtainGIRPort:
  134.     ; First we should check for valid input
  135.     cmp.l    #0,a0
  136.     ; We'll call ObtainGIPort anyway, even if the GadgetInfo is NULL..
  137.     beq        null1
  138.     
  139.     tst.l    ggi_Layer(a0)
  140.     beq        null1
  141.     
  142.     movem.l    a5/a6,-(sp)
  143.     move.l    a0,-(sp)
  144.  
  145.     ; non-NULL GadgetInfo->gi_Layer, try to lock it
  146.     move.l    GfxBase(pc),a6
  147.     move.l    ggi_Layer(a0),a5
  148.     jsr        AttemptLockLayerRom(a6)
  149.     move.l    (sp)+,a0
  150.     tst.w    d0
  151.     beq        null2
  152.  
  153.     ; Got a lock, free it
  154.     move.l    a0,-(sp)
  155.     move.l    ggi_Layer(a0),a5
  156.     jsr        UnlockLayerRom(a6)
  157.     
  158.     move.l    (sp)+,a0
  159.     movem.l    (sp)+,a5/a6
  160.  
  161. null1:
  162.     ; call original function
  163.     move.l    oldObtainGIRPort(pc),a1
  164.     jmp        (a1)
  165.     
  166. null2:
  167.     ; Attempt to lock the layer failed, return NULL
  168.     movem.l    (sp)+,a5/a6
  169.     move.l    #0,d0
  170.     rts
  171.  
  172. oldObtainGIRPort:
  173.     dc.l    0
  174.  
  175. DOSBase:
  176.     dc.l    0
  177. IBase:
  178.     dc.l    0
  179. GfxBase:
  180.     dc.l    0
  181.  
  182. dosname:
  183.     dc.b    "dos.library",0
  184. intuiname:
  185.     dc.b    "intuition.library",0
  186. gfxname:
  187.     dc.b    "graphics.library",0
  188. exitmsg:
  189.     dc.b    "NewObtainGIRPort exiting..",10,0
  190.  
  191.     dc.b    "$VER: NewObtainGIRPort 1.1 (29.1.95)",0
  192.  
  193.     END
  194.